home *** CD-ROM | disk | FTP | other *** search
/ The Atari Compendium / The Atari Compendium (Toad Computers) (1994).iso / files / prgtools / gnustuff / minix / update~4.z / update~4 / lib_stdio__flsbuf.c < prev    next >
Encoding:
C/C++ Source or Header  |  1989-09-06  |  2.6 KB  |  117 lines

  1. /*            _ f l s b u f
  2.  *
  3.  * This file contains three functions: _ioexit, _ioflush()
  4.  * and _flsbuf().
  5.  *
  6.  * _ioexit is empty and just provides a hook by which other
  7.  * functions can force the loading of this module (_ioflush()
  8.  * in particular).
  9.  *
  10.  * _ioflush() flushes all output buffers. This ensures that
  11.  * all streams are written out and closed. It's main purpose
  12.  * is to ensure that all streams are flushed on exit. exit()
  13.  * knows about the existence of _ioflush.
  14.  *
  15.  * _flsbuf allocates and flushes an output buffer. If no buffer
  16.  * has been previously allocated, one will be allocated. This function
  17.  * is intimately tied to the putc() macro in the stdio library.
  18.  *
  19.  * The function returns the character that was written to the
  20.  * file, otherwise EOF on error.
  21.  *
  22.  * Patchlevel 1.2
  23.  *
  24.  * Edit History:
  25.  * 06-Sep-1989    Declare c as unsigned char in _flsbuf().
  26.  * 05-Sep-1989    Change SETCLEANUP to SETIOFLUSH. Add _ioflush()
  27.  *        and _ioexit().
  28.  */
  29.  
  30. #include "stdiolib.h"
  31.  
  32. /*LINTLIBRARY*/
  33.  
  34. void _ioexit()
  35.  
  36. {
  37. }
  38.  
  39. void _ioflush()
  40.  
  41. {
  42.   int i;                /* slot index */
  43.  
  44.   for (i = 0; i < _NFILE; i++)
  45.     if (_iop[i] != NULL)
  46.       (void) fclose(_iop[i]);
  47. }
  48.  
  49. #ifdef __STDC__
  50. int _flsbuf(unsigned char c, FILE *fp)
  51. #else
  52. int _flsbuf(c, fp)
  53. unsigned char c;            /* character to write */
  54. FILE *fp;                /* stream */
  55. #endif
  56. {
  57.   char valid;                /* character still valid */
  58.   int length;                /* length of write */
  59.   int write();                /* write to channel */
  60.  
  61.   if (TESTFLAG(fp, _IOSTRING))
  62.     return (unsigned char) (*fp->_ptr++ = c);
  63.  
  64.   if (GETFLAG(fp, _IORW | _IOWRITE) == _IORW) {
  65.     if (! TESTFLAG(fp, _IOREAD))
  66.       SETFLAG(fp, _IOWRITE);
  67.     else {
  68.       if (TESTFLAG(fp, _IOEOF))
  69.     TOGGLEFLAG(fp, (_IOEOF | _IOREAD | _IOWRITE));
  70.     }
  71.   }
  72.  
  73.   if (GETFLAG(fp, (_IOWRITE | _IOERR)) != _IOWRITE)
  74.     return EOF;
  75.  
  76.   if (! HASBUFFER(fp)) {
  77.     if (_allocbuf(fp) < 0)
  78.       return EOF;
  79.     SETIOFLUSH();
  80.   }
  81.  
  82. /* Signal character valid if fully buffered output */
  83.   if (! TESTFLAG(fp, (_IONBF | _IOLBF)))
  84.     valid = 1;
  85.  
  86. /* Do the write here if unbuffered or line buffered */
  87.   else {
  88.     if (TESTFLAG(fp, _IONBF)) {
  89.       *fp->_ptr++ = c;
  90.       valid = 0;
  91.     }
  92.     else if (fp->_ptr < fp->_base + fp->_bufsiz) {
  93.       if ((*fp->_ptr++ = c) == '\n')
  94.     valid = 0;
  95.       else
  96.     return (unsigned char) c;
  97.     }
  98.     else
  99.       valid = 1;
  100.   }
  101.  
  102. /* Flush the current buffer load */
  103.   if ((length = fp->_ptr - fp->_base) != 0 &&
  104.       write(fp->_file, (char *) fp->_base, length) != length) {
  105.     SETFLAG(fp, _IOERR);
  106.     FLUSHNEXTWRITE(fp);
  107.     return EOF;
  108.   }
  109.  
  110. /* Reset the buffer pointers --- unbuffered => not valid */
  111.   INITWRITEBUFFER(fp);
  112.   if (valid)
  113.     *fp->_ptr++ = c;
  114.  
  115.   return (unsigned char) c;
  116. }
  117.